<?php
namespace app\service;
use alipay\AlipayOpenAppMiniTemplatemessageSendRequest;
use alipay\AlipayOpenAppQrcodeCreateRequest;
use alipay\AlipayOpenAuthTokenAppRequest;
use alipay\AlipayOpenPublicMessageSingleSendRequest;
use alipay\AlipaySystemOauthTokenRequest;
use alipay\AlipayTradeCloseRequest;
use alipay\AlipayTradeCreateRequest;
use alipay\AopClient;
use alipay\ZolozIdentificationCustomerCertifyzhubQueryRequest;
use alipay\ZolozIdentificationUserWebQueryRequest;

//查询物业token
//换取授权访问令牌
//生成订单
//小程序模板信息
//创建二维码

//单发模板消息接口
class jinchengService{

    const LIFEPUBKEYPATH= 'C:\phpStudy\WWW\zaozao\pathappid\jincheng\public.txt';
    const XCXPRIKEYPATH= 'C:\phpStudy\WWW\zaozao\jinchengprikey.txt';
    const XCXPUBKEYPATH= 'C:\phpStudy\WWW\zaozao\jinchengpubkey.txt';

    private $app_id;//支付宝分配给开发者的应用
    private $method;//接口名称
    private $format;//仅支持JSON  no
    private $charset = 'UTF-8';//请求使用的编码格式，如utf-8,gbk,gb2312等
    private $sign_type = 'RSA2';//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
    private $sign;//商户请求参数的签名串  no
    private $timestamp;//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
    private $version = '1.0';//调用的接口版本，固定为：1.0
    private $app_auth_token;//详见应用授权概述 no
    private $biz_content;//


     //XIOA
    public function xcxgetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2021001163687196';
        $aop->rsaPrivateKey = file_get_contents(self::XCXPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::XCXPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
    public function lifegetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018032102418565';
        $aop->rsaPrivateKey = file_get_contents(self::XCXPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::LIFEPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
    //小程序换取授权访问令牌
    public function xcxoauthToken($code){
        $aop = $this->xcxgetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ($request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";   
        }
        return $result->$responseNode;
    }
    


    //
    public function appletSendMsg($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new AlipayOpenAppMiniTemplatemessageSendRequest ();
        $request->setBizContent("{" .
        "\"to_user_id\":\"$data->user_id\"," .
        "\"form_id\":\"$data->form_id\"," .
        "\"user_template_id\":\"$data->template_id\"," .
        "\"page\":\"$data->page\"," .
        "\"data\":\"$data->data\"" .
        "}");
        $result = $aop->execute ( $request); 
         
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    

    public function payMsg($data){
        $data = json_decode($data);
        $aop = $this->lifegetPublicContent();
        $request = new AlipayOpenPublicMessageSingleSendRequest ();
        $request->setBizContent("{" .
            "\"to_user_id\":\"$data->user_id\"," .
            "\"template\":{" .
                "\"template_id\":\"952c8c29d4824c45b5d4b85f63b90b02\"," .
                "\"context\":{" .
                    "\"head_color\":\"#85be53\"," .
                    "\"url\":\"$data->url\"," .
                    "\"action_name\":\"查看详情\"," .
                    "\"keyword1\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->owner_name\"" .
                        "        }," .
                        "\"keyword2\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->pay_type\"" .
                        "        }," .
                        "\"keyword3\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->pay_money\"" .
                        "        }," .
                        "\"keyword4\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->acct_period\"" .
                        "        }," .
                        "\"first\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->title\"" .
                        "        }," .
                        "\"remark\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->remark\"" .
                        "        }" .
                    "      }" .
                "    }" .
            "  }");
        $result = $aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        return $result->$responseNode;
    }

    

    public function testMsg($data){
        $data = json_decode($data);
        $aop = $this->lifegetPublicContent();
        $request = new AlipayOpenPublicMessageSingleSendRequest ();
        $request->setBizContent("{" .
            "\"to_user_id\":\"$data->user_id\"," .
            "\"template\":{" .
                "\"template_id\":\"952c8c29d4824c45b5d4b85f63b90b02\"," .
                "\"context\":{" .
                    "\"head_color\":\"#85be53\"," .
                    "\"url\":\"$data->url\"," .
                    "\"action_name\":\"查看详情\"," .
                    "\"keyword1\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->owner_name\"" .
                        "        }," .
                        "\"keyword2\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->pay_type\"" .
                        "        }," .
                        "\"keyword3\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->pay_money\"" .
                        "        }," .
                        "\"keyword4\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->acct_period\"" .
                        "        }," .
                        "\"first\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->title\"" .
                        "        }," .
                        "\"remark\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->remark\"" .
                        "        }" .
                    "      }" .
                "    }" .
            "  }");
        $result = $aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        return $result->$responseNode;
    }
    //会员获取积分提醒
    public function getPointMsg($data){
        $data = json_decode($data);
        $aop = $this->lifegetPublicContent();
        $request = new AlipayOpenPublicMessageSingleSendRequest ();
        $request->setBizContent("{" .
            "\"to_user_id\":\"$data->user_id\"," .
            "\"template\":{" .
                "\"template_id\":\"952c8c29d4824c45b5d4b85f63b90b02\"," .
                "\"context\":{" .
                    "\"head_color\":\"#85be53\"," .
                    "\"url\":\"$data->url\"," .
                    "\"action_name\":\"查看详情\"," .
                    "\"keyword1\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->shop_name\"" .
                        "        }," .
                        "\"keyword2\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->pay_money\"" .
                        "        }," .
                        "\"keyword3\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->give_point\"" .
                        "        }," .
                        "\"keyword4\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->time\"" .
                        "        }," .
                        "\"keyword5\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->point\"" .
                        "        }," .
                        "\"first\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->title\"" .
                        "        }," .
                        "\"remark\":{" .
                            "\"color\":\"#85be53\"," .
                            "\"value\":\"$data->remark\"" .
                        "        }" .
                    "      }" .
                "    }" .
            "  }");
        $result = $aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        return $result->$responseNode;
    }
    
}